package com.wangzifan.controller;

import com.wangzifan.service.StudentService;
import com.wangzifan.entity.PageBean;
import com.wangzifan.entity.Result;
import com.wangzifan.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
//    @GetMapping
//    public Result queryStudent(@RequestBody Student student) {
//        log.info("条件查询学生信息");
//        List<Student> students = studentService.queryStudent(student);
//        return Result.success(students);
//    }
    @GetMapping
    public Result queryStudent(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               String sno,String sname,Short gender) {
        log.info("分页查询, 参数: {},{},{},{},{}",page,pageSize,sno,sname,gender);
        PageBean pageBean = studentService.page(page,pageSize,sno,sname,gender);
        return Result.success(pageBean);
    }

    /**
     * 批量删除学生
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteStudent(@PathVariable List<Integer> ids) {
        log.info("批量删除学生，ids：{}",ids);
        studentService.deleteStudent(ids);
        return Result.success();
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping
    public Result addStudent(@RequestBody Student student) {
        log.info("添加学生，学生：{}",student);
        studentService.addStudent(student);
        return Result.success();
    }

    /**
     * 根据id查询学生，回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryStudentById(@PathVariable Integer id) {
        log.info("查询id为：{}的学生",id);
        Student student = studentService.queryStudentById(id);
        return Result.success(student);
    }

    /**
     * 更新学生
     * @param student
     * @return
     */
    @PutMapping
    public Result updateStudent(@RequestBody Student student) {
        log.info("更新学生， 学生：{}",student);
        studentService.updateStudent(student);
        return Result.success();
    }
}
